<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="联系人">
              <a-input placeholder="请输入联系人" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <!--          <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
          <!--            <a-form-item label="联系人电话">-->
          <!--              <a-input placeholder="请输入联系人电话" v-model="queryParam.telephoneIds"></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="级别">
              <j-dict-select-tag placeholder="请选择级别" v-model="queryParam.level"
                                 dictCode="constacts_level" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!--              <a @click="handleToggleSearch" style="margin-left: 8px">-->
              <!--                {{ toggleSearchStatus ? '收起' : '展开' }}-->
              <!--                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
              <!--              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('联系人表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
                     @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="importance" slot-scope="text,record">
          <div>
            <a-rate :value="text" disabled />
          </div>
        </template>
        <template slot="intimacy" slot-scope="text,record">
          <div>
            <a-rate :value="text" disabled />
          </div>
        </template>
        <template slot="customerId_dictText" slot-scope="text,record">
          <a @click="handleCustomerDetail(record)">{{ text }}</a>
        </template>
        <template slot="name" slot-scope="text,record">
          <a @click="handleDetail(record)">{{ text }}</a>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="pcaSlot" slot-scope="text">
          <div>{{ getPcaText(text) }}</div>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <!--        <template slot="allphone" slot-scope="text, record">-->
        <!--          <a @click="getAllPhone(record)">查看全部</a>-->
        <!--        </template>-->

<!--        <template slot="allphone" slot-scope="text, record">-->
<!--          <div>-->
<!--            <a @click="getAllPhone(record)">查看全部</a>-->
<!--          </div>-->
<!--        </template>-->
        <template slot="type" slot-scope="text,record">
          <div class="table-warp">

            {{ record.telephones.length>0?record.telephones[0].typeText:'' }}
            <!--            <a-popover trigger="click" class="chakanquanbu">
                          <template slot="content">
                            <a-table :columns="seecolumns" :data-source="record.telephones" size="small" bordered
                                     :pagination="false">

                            </a-table>
                          </template>
                          <a-button type="link">
                            查看全部
                          </a-button>
                        </a-popover>-->
          </div>
        </template>
        <template slot="phone" slot-scope="text,record">
          <div class="table-warp">
            {{ record.telephones.length>0?record.telephones[0].phone:'' }}
            <a-popover trigger="click" class="chakanquanbu">
              <template slot="content">
                <a-table :columns="seecolumns" :data-source="record.telephones" size="small" bordered
                         :pagination="false">

                </a-table>
              </template>
              <a-button type="link">
                查看全部
              </a-button>
            </a-popover>
            <!--            <a class="chakanquanbu">查看全部</a>-->
          </div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!--    底部按钮-->
    <div class="buttonStyle">
      <div class="ant-alert ant-alert-info">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a-divider class="btn" type="vertical" />
        <a @click="onClearSelected">清空</a>
        <a-button v-has="'contacts:transfer_user'" type="default"
                  @click="MAction(false,'/contacts/contacts/updateUserId',2,1260)">移交</a-button>
        <a-button type="default" @click="MAction(false,'/contacts/contacts/updateDirector',3,1260)">添加负责人</a-button>
        <a-button type="default" @click="MAction(false,'/contacts/contacts/deleteDirector',6,1260)">删除负责人</a-button>
        <a-button type="default" @click="MAction(true,'/contacts/contacts/updateDirector',5,1260)">添加协同人</a-button>
        <a-button type="default" @click="MAction(true,'/contacts/contacts/deleteDirector',4,1260)">删除协同人</a-button>






      </div>
    </div>
    <div>
      <a-modal
        v-model="visible"
        :visible="visible"
        title="全部联系方式"
        :ok-button-props="{ props: { disabled: true } }"
        :cancel-button-props="{ props: { disabled: true } }"
        @ok="handleOk"
      >
        <div class="table">
          <div class="top">
            <div class="top-left">类型</div>
            <div class="top-right">号码</div>
          </div>
          <div v-for="(item,index) in allphones" :key="index" class="top" style="background-color: #ffffff">
            <div class="top-left"> {{ item.typeText }}</div>
            <div class="top-right">{{ item.phone }}</div>
          </div>

        </div>

      </a-modal>
      <!--      <a-drawer-->
      <!--        title="全部联系方式"-->
      <!--        placement="right"-->
      <!--        :closable="false"-->
      <!--        :visible="visible"-->
      <!--        :after-visible-change="afterVisibleChange"-->
      <!--        @close="onClose"-->
      <!--        width="600"-->
      <!--      >-->
      <!--        <table border="1">-->
      <!--          <tr>-->
      <!--            <td>类型</td>-->
      <!--            <td>号码</td>-->
      <!--          </tr>-->
      <!--          <tr v-for="(item,index) in allphones" :key="index">-->
      <!--            <td> {{item.typeText}}</td>-->
      <!--            <td>{{item.phone}}</td>-->
      <!--          </tr>-->

      <!--        </table>-->
      <!--      </a-drawer>-->
    </div>
    <contacts-modal ref="modalForm" @ok="modalFormOk"></contacts-modal>
    <CustomerModal ref="CustomerModal"></CustomerModal>
    <JSelectUserByDepModalCustom ref="MAmodel" :modalWidth="MAwidth" :multi="MAisMulti"
                                 @ok="MACallback"></JSelectUserByDepModalCustom>
  </a-card>

</template>

<script>
import JSelectUserByDepModalCustom from '@/components/jeecgbiz/modal/JSelectUserByDepModalCustom'
import CustomerModal from '@/views/customer/modules/CustomerModal.Style#Drawer'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ContactsModal from './modules/ContactsModal.Style#Drawer'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import Area from '@/components/_util/Area'
import { getAction, httpAction, deleteAction, getFileAccessHttpUrl, postAction } from '@/api/manage'


export default {
  name: 'ContactsList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ContactsModal,
    CustomerModal,
    JSelectUserByDepModalCustom
  },
  data() {
    return {
      showCustomers: false,
      visible: false,
      allphones: [],
      //名单选择时 多选还是单选
      MAisMulti: false,
      //选择名单回调接口
      MAcallBackUrl: '',
      //名单参数类型    1===移交 2===负责人 3协同人 4===分配
      MAtype: 0,
      //弹框宽度
      MAwidth: 1260,
      description: '联系人表管理页面',
      seecolumns: [
        {
          title: '类型',
          dataIndex: 'typeText',
          align: 'center',
          key: 'typeText'
        },
        {
          title: '电话',
          dataIndex: 'phone',
          align: 'center',
          key: 'phone'
        }
      ],
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerIdDictText',
          scopedSlots: { customRender: 'customerId_dictText' }
        },
        {
          title: '联系人',
          align: 'center',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '联系电话',
          align: 'left',
          dataIndex: 'telephones',
          children: [
            {
              title: '类型',
              dataIndex: 'type',
              key: 'typeText',
              width: 150,
              align: 'left',
              scopedSlots: { customRender: 'type' }
            },
            {
              title: '号码',
              dataIndex: 'phone',
              key: 'phone',
              align: 'left',
              width: 150,
              scopedSlots: { customRender: 'phone' }
            }
          ]

        },
        {
          title: '邮箱',
          align: 'center',
          dataIndex: 'email'
        },
        {
          title: 'QQ',
          align: 'center',
          dataIndex: 'qq'
        },
        {
          title: '地址',
          align: 'center',
          dataIndex: 'address',
          scopedSlots: { customRender: 'pcaSlot' }
        },
        {
          title: '地址详情',
          align: 'center',
          dataIndex: 'addressInfo'
        },
        {
          title: '邮编',
          align: 'center',
          dataIndex: 'postalCode'
        },
        {
          title: '性别',
          align: 'center',
          dataIndex: 'gender_dictText'
        },
        {
          title: '生日',
          align: 'center',
          dataIndex: 'birthday'
        },
        {
          title: '爱好',
          align: 'center',
          dataIndex: 'hobby'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '部门',
          align: 'center',
          dataIndex: 'department'
        },
        {
          title: '职务',
          align: 'center',
          dataIndex: 'post'
        },
        {
          title: '级别',
          align: 'center',
          dataIndex: 'level_dictText'
        },
        {
          title: '负责人',
          align: 'left',
          width: 150,
          dataIndex: 'directorIds_dictText'
        },
        {
          title: '协同人',
          align: 'left',
          width: 150,
          dataIndex: 'collaboratorIds_dictText'
        },
        {
          title: '重要程度',
          align: 'center',
          dataIndex: 'importance',
          scopedSlots: { customRender: 'importance' }
        },
        {
          title: '决策关系',
          align: 'center',
          dataIndex: 'relationBetween_dictText'
        },
        {
          title: '亲密度',
          align: 'center',
          dataIndex: 'intimacy',
          scopedSlots: { customRender: 'intimacy' }
        },
        {
          title:'创建人',
          align:"center",
          dataIndex: 'createBy'
        },
        {
          title:'创建时间',
          align:"center",
          dataIndex: 'createTime'
        },
        {
          title:'更新时间',
          align:"center",
          dataIndex: 'updateTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        queryUrl: '/customer/customer/list',
        list: '/contacts/contacts/list',
        delete: '/contacts/contacts/delete',
        deleteBatch: '/contacts/contacts/deleteBatch',
        exportXlsUrl: '/contacts/contacts/exportXls',
        importExcelUrl: 'contacts/contacts/importExcel',
        getAllPhone: '/contacts/contacts/queryTelephoneIds',
        updateCollaboratorIds: '/contacts/contacts/updateUserId',//移交
        saveUser: '/contacts/contacts/deleteDirector',//添加协同人
        deleteUser: '/customer/customer/deleteUser'//删除协同人

      },
      dictOptions: {},
      pcaData: '',
      superFieldList: []
    }
  },
  created() {
    this.pcaData = new Area()
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleCustomerDetail(record) {
      this.loading = true
      getAction(this.url.queryUrl, { id: record.customerId }).then(res => {
        console.log('客户信息', res)
        if (res.success) {
          this.$refs.CustomerModal.dongtai(record.customerId)
          this.$refs.CustomerModal.dongtaiShow = true
          this.$refs.CustomerModal.disableSubmit = true
          this.$refs.CustomerModal.edit(res.result.records[0])

        } else {
          this.$message.error(res.message)
        }

      }).finally(() => {
        this.loading = false
      })
      // this.$refs.CustomerModal.edit(record)
    },
    showModal() {
      this.visible = true
    },
    handleOk(e) {
      console.log(e)
      this.visible = false
    },
    handleCancel(e) {
      console.log(e)
      this.visible = false
    },

    // afterVisibleChange(val) {
    //   console.log('visible', val);
    // },
    // showDrawer() {
    //   this.visible = true;
    // },
    // onClose() {
    //   this.visible = false;
    // },
    getAllPhone(record) {
      this.loading = true
      this.visible = true
      console.log('获取全部电话', record)
      getAction(this.url.getAllPhone, { telephoneIds: record.telephoneIds }).then(res => {
        console.log('全部电话', res)
        if (res.success) {
          this.loading = false
          this.allphones = res.result
        } else {
        }

      })
    },
    getPcaText(code) {
      return this.pcaData.getText(code)
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({
        type: 'string',
        value: 'customerId',
        text: '客户ID',
        dictTable: 'biz_customer',
        dictText: 'name',
        dictCode: 'id'
      })
      fieldList.push({ type: 'string', value: 'name', text: '联系人', dictCode: '' })
      fieldList.push({ type: 'string', value: 'telephones', text: '电话IDS', dictCode: '' })
      fieldList.push({ type: 'string', value: 'email', text: '邮箱', dictCode: '' })
      fieldList.push({ type: 'string', value: 'qq', text: 'QQ', dictCode: '' })
      fieldList.push({ type: 'pca', value: 'address', text: '地址' })
      fieldList.push({ type: 'string', value: 'addressInfo', text: '地址详情', dictCode: '' })
      fieldList.push({ type: 'string', value: 'postalCode', text: '邮编', dictCode: '' })
      fieldList.push({ type: 'int', value: 'gender', text: '性别(1.男 2.女 0.未知)', dictCode: 'Gender' })
      fieldList.push({ type: 'string', value: 'birthday', text: '生日', dictCode: '' })
      fieldList.push({ type: 'string', value: 'hobby', text: '爱好', dictCode: '' })
      fieldList.push({ type: 'string', value: 'remark', text: '备注', dictCode: '' })
      fieldList.push({ type: 'sel_depart', value: 'department', text: '部门' })
      fieldList.push({ type: 'string', value: 'post', text: '职务', dictCode: '' })
      fieldList.push({ type: 'int', value: 'level', text: '级别(1.高层 2.中层 3.低层)', dictCode: 'constacts_level' })
      fieldList.push({ type: 'int', value: 'importance', text: '重要程度(1-5)', dictCode: '' })
      fieldList.push({
        type: 'sel_search',
        value: 'collaboratorIds',
        text: '协同人IDS',
        dictTable: 'sys_user',
        dictText: 'username',
        dictCode: 'id'
      })
      fieldList.push({
        type: 'sel_search',
        value: 'directorIds',
        text: '负责人IDS',
        dictTable: 'sys_user',
        dictText: 'username',
        dictCode: 'id'
      })
      fieldList.push({
        type: 'string',
        value: 'relationBetween',
        text: '决策关系(1.关键决策人 2.分项决策人 3.商务决策人 4.技术决策人 5.财务决策人 6.使用人 7.意见影响人)',
        dictCode: 'consacts_relation'
      })
      fieldList.push({ type: 'string', value: 'intimacy', text: '亲密度', dictCode: '' })
      fieldList.push({ type: 'datetime', value: 'createTime', text: '创建时间' })
      this.superFieldList = fieldList
    },
    MAction(MAisMulti, MAcallBackUrl, MAtype, MAwidth) {
      //Member action
      //设置多选还是单选
      // this.MAisMulti = false
      this.MAisMulti = MAisMulti
      //设置回调接口
      // this.MAcallBackUrl  = '/quotation/quotation/updateDirector'
      this.MAcallBackUrl = MAcallBackUrl
      //设置回调接口参数
      // this.MAtype = 1
      this.MAtype = MAtype
      //弹框宽度
      // this.MAwidth = 1260
      this.MAwidth = MAwidth
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请至少或者最多选择一条记录！')
        return
      }
      this.$refs.MAmodel.showModal()
    },
    //移交分配
    MACallback(e) {
      this.loading = true
      console.log('e', e)
      console.log('this.selectedRowKeys', this.selectedRowKeys)
      let ids = this.selectedRowKeys.join(',')
      let obj = {}
      if (this.MAtype == 1 || this.MAtype == 2) {
        switch (this.MAtype) {
          case 1://分配
            obj = { id: ids, directorIds: e[0].id }
            break
          case 2://移交
            obj = { id: ids, userId: e[0].id }
            break
        }
        postAction(this.MAcallBackUrl, obj).then(res => {
          if (res.success) {
            this.selectedRowKeys = []
            this.$message.success(res.message)
            this.loading = false
            this.loadData()
          } else {
            this.$message.error(res.message)
            this.loading = false
          }
        })
      } else {
        switch (this.MAtype) {
          case 3://添加负责人
            obj = { id: ids, directorIds: e[0].id }
            break
          case 4://删除协同人
            console.log('e[0].id',e)
            let arrdelect = []
            e.forEach(res=>{
              arrdelect.push(res.id)
            })
            obj = { id: ids, collaboratorIds: arrdelect.join(',') }
            break
          case 5://添加协同人
            console.log('e[0].id',e)
            let arr = []
            e.forEach(res=>{
              arr.push(res.id)
            })
            obj = { id: ids, collaboratorIds: arr.join(',') }
            break
          case 6://删除负责人
            obj = { id: ids, directorIds: e[0].id }
            break
        }
        postAction(this.MAcallBackUrl, obj).then(res => {
          if (res.success) {
            this.selectedRowKeys = []
            this.$message.success(res.message)
            this.loading = false
            this.loadData()
          } else {
            this.$message.error(res.message)
            this.loading = false
          }
        })
      }
    }
  }
}
</script>
<style scoped lang="less">
@import '~@assets/less/common.less';

.buttonStyle {

  button {
    margin-left: 10px;
  }
}

.ant-table table {
  table-layout: fixed;
}

.table-warp {
  width: 100%;
  word-break: break-all;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1; /* 这里是超出几行省略 */
  overflow: hidden;
  position: relative;
}

.chakanquanbu {
  transition: all 0.3s ease-out;
  opacity: 0;
  position: absolute;
  right: -16px;
  top: -4px;
  background: #e6f7ff;
}

/deep/ .ant-table-tbody > tr:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected) > td {
  .chakanquanbu {
    opacity: 1;
  }
}


.table {
  background-color: #00DB00;
  width: 100%;
  text-align: center;
}

.top {
  display: flex;
  flex-direction: row;
  width: 100%;
  height: 40px;
  margin: auto;
  background-color: #fafafa;
  border-top: #eeeeee 1px solid;
  border-left: #eeeeee 1px solid;
}

.top-left {
  display: block;
  width: 30%;
  height: 40px;
  line-height: 40px;
  align-items: center;
  border-right: #eeeeee 1px solid;
  border-bottom: #eeeeee 1px solid;
}

.top-right {
  width: 70%;
  display: block;
  height: 40px;
  line-height: 40px;
  align-items: center;
  border-right: #eeeeee 1px solid;
  border-bottom: #eeeeee 1px solid;
}
.chakanquanbu {
  transition: all 0.3s ease-out;
  opacity: 0;
  position: absolute;
  right: -16px;
  top: -4px;
  background: #e6f7ff;
}
.table-warp {
  width: 100%;
  word-break: break-all;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1; /* 这里是超出几行省略 */
  overflow: hidden;
  position: relative;
}




</style>